﻿

'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose: This provides a base record for each of the physical ports that we
' work with from the product data table. It is built when we perform the
' verify process against the product data master/stencils. There should be one
' of these root records for each component port 
' The primary key for these records is productkey/portname/portview
'
'
'*******************************************************************


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging


Imports System
Imports System.ComponentModel
Imports System.Collections.Generic


Public Class dc_PortBase

#Region " Constructor "

    Public Sub New()
        initProperties()
    End Sub

    Public Sub New _
        (ByVal intProdKey As Int32, _
         ByVal intProdView As Int16, _
         ByVal strPortName As String)

        initProperties()
        portProductKey = intProdKey
        portProductView = intProdView
        portName = strPortName
        ' anything past the second underscore is the portname (since it may have embedded underscores)
        portbaseKey = CStr(portProductKey) & "_" & CStr(portProductView) & "_" & strPortName

    End Sub

#End Region

#Region " Enumerations "

    '
    ' placeholders that mimic the link types
    '
    Public Enum PortTech
        techUnknown = -1
        techVendor = 0          ' the conn type
        techCopper = 31         ' ethernet
        techFiber = 32
        techCoax = 33
        techSerial = 34
        techUSB = 35
    End Enum

    '
    ' what type of connector, ties into the custom stencil
    ' initially only the fibre and copper are implemented
    '
    Public Enum PortConn
        connUnknown = -1
        connVendor = 0
        connFibre = 1
        ' use the default rj45 drawing for copper 
        connCopper = 2
    End Enum

    '
    ' this is our active passive flag for the port (if active then network attributes may be available)
    '
    Public Enum PortTraf
        trafUnknown = -1
        trafPassive = 0
        trafActive = 1
    End Enum


#End Region

#Region " Properties and Fields "


    Private m_portproductkey As Int32
    Public Const constPortProductKey As Int32 = -1
    Public Property portProductKey() As Int32
        Get
            Return Me.m_portproductkey
        End Get
        Set(ByVal value As Int32)
            Me.m_portproductkey = value
        End Set
    End Property

    Private m_portproductview As dc_Product.View
    Public Const constPortProductView As dc_Product.View = dc_Product.View.viewUnknown
    Public Property portProductView() As dc_Product.View
        Get
            Return Me.m_portproductview
        End Get
        Set(ByVal value As dc_Product.View)
            If Not TypeDescriptor.GetConverter(GetType(dc_Product.View)).IsValid(value) Then
                'Throw New ArgumentException()
                value = dc_Product.View.viewUnknown
            End If
            Me.m_portproductview = value
        End Set
    End Property

    Private m_portname As String
    Public Const constPortName As String = "na"
    Public Property portName() As String
        Get
            Return Me.m_portname
        End Get
        Set(ByVal value As String)
            m_portname = value
            m_wellformedname = isWellFormedName(value)
            If m_wellformedname = True Then
                Dim instance As String = value
                Dim charSeparators() As Char = {"_"c}
                Dim count As Integer = 3
                Dim options As StringSplitOptions = StringSplitOptions.None
                Dim returnValue As String()

                returnValue = instance.Split(charSeparators, _
                        count, options)
                portTechnology = CInt(returnValue(3))
            End If
        End Set
    End Property

    Private m_porttechnology As PortTech
    Public Const constPortTechnology As PortTech = PortTech.techUnknown
    Public Property portTechnology() As PortTech
        Get
            Return Me.m_porttechnology
        End Get
        Set(ByVal value As PortTech)
            If Not TypeDescriptor.GetConverter(GetType(PortTech)).IsValid(value) Then
                'Throw New ArgumentException()
                value = portTechnology.techUnknown
            End If
            Me.m_porttechnology = value
        End Set
    End Property


    Private m_portconnector As PortConn
    Public Const constPortConnector As PortConn = PortConn.connUnknown
    Public Property portConnector() As PortConn
        Get
            Return Me.m_portconnector
        End Get
        Set(ByVal value As PortConn)
            If Not TypeDescriptor.GetConverter(GetType(PortConn)).IsValid(value) Then
                'Throw New ArgumentException()
                value = PortConn.connUnknown
            End If
            Me.m_portconnector = value
        End Set
    End Property


    Private m_porttraffic As PortTraf
    Public Const constPortTraffic As PortTraf = PortTraf.trafUnknown
    Public Property portTraffic() As PortTraf
        Get
            Return Me.m_porttraffic
        End Get
        Set(ByVal value As PortTraf)
            If Not TypeDescriptor.GetConverter(GetType(PortTraf)).IsValid(value) Then
                'Throw New ArgumentException()
                value = PortTraf.trafUnknown
            End If
            Me.m_porttraffic = value
        End Set
    End Property

    Private m_wellformedname As Boolean
    Public Const constPortWellFormedName As Boolean = False
    Public Property portWellFormedName() As Boolean
        Get
            Return Me.m_wellformedname
        End Get
        Set(ByVal value As Boolean)
            m_wellformedname = value
        End Set
    End Property

    Private m_portbuilddate As Date
    Public Const constPortBuildDate As Date = #1/1/2001#
    Public Property portBuildDate() As Date
        Get
            Return Me.m_portbuilddate
        End Get
        Set(ByVal value As Date)
            m_portbuilddate = value
        End Set
    End Property

    Private m_portbaseKey As String
    Public Property portbaseKey As String
        Get
            Return m_portbaseKey
        End Get
        Set(ByVal value As String)
            m_portbaseKey = value
        End Set
    End Property



#End Region

#Region " Methods "


    '
    '
    '
    Public Sub initProperties()
        Try
            ' init the variables
            portProductKey = constPortProductKey
            portName = constPortName
            portTechnology = constPortTechnology
            portConnector = constPortConnector
            portTraffic = constPortTraffic
            portProductView = constPortProductView
            portWellFormedName = constPortWellFormedName
            portBuildDate = Now

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    ' pass in a porttech and return string 
    '
    Public Function getPortTechnology _
        (ByVal intTech As PortTech) _
        As String


        Dim strReturn As String = ""

        ' if an invalid link type is passed in then make it an unknown so we do not fail
        If Not TypeDescriptor.GetConverter(GetType(PortTech)).IsValid(intTech) Then
            intTech = PortTech.techUnknown
        End If

        Dim myName As PortTech = intTech
        strReturn = myName.ToString

        Return strReturn
    End Function

    '
    ' pass in a portconn type and return a string
    '
    Public Function getPortConnector _
            (ByVal intConn As PortConn) _
            As String

        Dim strReturn As String = ""

        ' if an invalid link type is passed in then make it an unknown so we do not fail
        If Not TypeDescriptor.GetConverter(GetType(PortConn)).IsValid(intConn) Then
            intConn = PortConn.connUnknown
        End If

        Dim myName As PortConn = intConn
        strReturn = myName.ToString

        Return strReturn

    End Function

    '
    ' used to test port and slot names
    '
    Private Function isWellFormedName(ByVal strName As String) As Boolean

        Dim blnReturn As Boolean = False

        Try

            If strName = "" Then GoTo ExitHandler
            Dim strType As String

            ' parse out the port name
            ' first string is the record field name
            ' second string is the record key
            Dim instance As String = strName
            Dim charSeparators() As Char = {"_"c}
            Dim count As Integer = 3
            Dim options As StringSplitOptions = StringSplitOptions.None
            Dim returnValue As String()

            returnValue = instance.Split(charSeparators, _
                    count, options)

            If returnValue.Count <> 3 Then GoTo ExitHandler

            strType = LCase(returnValue(1))
            ' rule 1 has to start with port rather than slot/conn
            If strType <> "port" Then GoTo ExitHandler

            ' the final field must be valid technology
            Dim intTech As Integer = CInt(returnValue(3))
            If Not TypeDescriptor.GetConverter(GetType(PortTech)).IsValid(intTech) Then
                GoTo ExitHandler
            End If

            ' if you made it this far it's ok
            blnReturn = True
ExitHandler:

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Global Policy")
            If (rethrow) Then
                ' throw original exception
                Throw
            End If
        Finally
        End Try

        Return blnReturn


    End Function



#End Region


End Class
